from flask import Blueprint, jsonify

api = Blueprint('api', __name__)


@api.errorhandler(400)
def handler_400(e):
    return jsonify(stat=0, err=0, msg='Bad Request'), 400


@api.errorhandler(401)
def handler_401(e):
    return jsonify(stat=0, err=1, msg='Unauthorized'), 403


@api.errorhandler(403)
def handler_403(e):
    return jsonify(stat=0, err=2, msg='Forbidden'), 403


@api.errorhandler(404)
def handler_404(e):
    return jsonify(stat=0, err=3, msg='Not Found'), 404


@api.route('/github/push', methods=['POST'])
def github_push():
    import os, subprocess
    subprocess.Popen('git pull', cwd=os.getcwd(), shell=True)
    return 'ok'


# Define error code
ERROR_GLOBAL = 0000
ERROR_USER = 1000
ERROR_ANSWER = 2000
ERROR_COMMENT = 3000
ERROR_PROBLEM = 4000
ERROR_CLASS = 5000
ERROR_VIDEO = 6000
ERROR_STUDIO = 7000
ERROR_ARTICLE = 8000
ERROR_PIN = 9000

class GlobalError:
    INVALID_USER_ID = {
        'err': ERROR_GLOBAL + 1,
        'msg': 'Invalid User ID'
    }
    INVALID_PROBLEM_ID = {
        'err': ERROR_GLOBAL + 2,
        'msg': 'Invalid Problem ID'
    }
    INVALID_ANSWER_ID = {
        'err': ERROR_GLOBAL + 3,
        'msg': 'Invalid Answer ID'
    }
    REFUSED_FILE_FORMAT = {
        'err': ERROR_GLOBAL + 4,
        'msg': 'Refused file format'
    }
    PARAMETERS_INVALID = {
        'err': ERROR_GLOBAL + 5,
        'msg': 'Parameters Invalid'
    }
    INVALID_VIDEO_ID = {
        'err': ERROR_GLOBAL + 6,
        'msg': 'Invalid Video ID'
    }
    INVALID_STUDIO_ID = {
        'err': ERROR_GLOBAL + 7,
        'msg': 'Invalid Studio ID'
    }
    INVALID_ARTICLE_ID = {
        'err': ERROR_GLOBAL + 8,
        'msg': 'Invalid Article ID'
    }


import answer
import article
import classification
import comment
import good
import image
import merchant
import problem
import studio
import user
import video
import pin
